/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.view;

import org.chaoticengine.ast.GlobalData;
import org.chaoticengine.ast.model.AirShip;
import org.chaoticengine.ast.model.Location;
import org.chaoticengine.ast.pattern.Model;
import org.chaoticengine.ast.pattern.ViewController;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class LocationMapView extends ViewController<Location> {
    protected Location location = null;
    protected int tileW = TileFactory.getInstance().getTileWidth();
    protected int tileH = TileFactory.getInstance().getTileHeight();
    protected Image image = null;

    public LocationMapView() {}

    public void refresh(Location model) {
        this.location = model;

        image = null;
        Tile t = TileFactory.getInstance().getTile(this.location.getName());
        image = t.getImage();

        int x = model.getX() * tileW;
        int y = model.getY() * tileH;
        setPosition(new Vector2f(x, y));
        setBoundingRectangle(new Rectangle(x, y, tileW, tileH));
    }

    public Location getModel() {
        return(location);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) {}

    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        if (image != null) {
            if (selected) {
                grphcs.setLineWidth(4.0f);
            } else {
                grphcs.setLineWidth(2.0f);
            }

            grphcs.setColor(Color.black);
            grphcs.fillRoundRect(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), 8);

            if (selected) {
                grphcs.setColor(Color.red);
            } else {
                grphcs.setColor(Color.lightGray);
            }
            grphcs.drawRoundRect(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), 8);
            grphcs.drawImage(image, position.x, position.y);
        }
    }

    public int getTileWidth() {
        return(tileW);
    }

    public int getTileHeight() {
        return(tileH);
    }

    @Override
    public void onMouseClick(int x, int y, int button) {
    }

    @Override
    public void onMouseEnter() {
        //System.out.println("Location('" + location.getName() + "'): MOUSE_ENTER");
    }

    @Override
    public void onMouseExit() {
        //System.out.println("Location('" + location.getName() + "'): MOUSE_EXIT");
    }

    @Override
    public void onMouseMove(int oldx, int oldy, int newx, int newy) {
    }

    @Override
    public void onMouseRelease(int x, int y, int button) {
        if (button == Input.MOUSE_LEFT_BUTTON) {
            GlobalData.getInstance().setSelected(this);
        } else if (button == Input.MOUSE_RIGHT_BUTTON) {
            ViewController<? extends Model> vc = GlobalData.getInstance().getSelected();
            if (vc != null) {
                Model mdl = vc.getModel();
                if (mdl instanceof AirShip) {
                    ((AirShip) mdl).goTo(location.getX(), location.getY());
                }
            }
        }
    }

    @Override
    public void onMouseWheelDown() {}

    @Override
    public void onMouseWheelUp() {}

    @Override
    public void keyPressed(int key, char c) {}

    @Override
    public void keyReleased(int key, char c) {}

    @Override
    public void onDeselect() {
    }

    @Override
    public void onSelect() {
    }
}
